package cn.iocoder.yudao.module.infrastructure.file.controller.app;

import cn.hutool.core.io.IoUtil;
import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.infrastructure.file.controller.admin.vo.file.FileCreateReqVO;
import cn.iocoder.yudao.module.infrastructure.file.controller.admin.vo.file.FilePresignedUrlRespVO;
import cn.iocoder.yudao.module.infrastructure.file.controller.app.vo.AppFileUploadReqVO;
import cn.iocoder.yudao.module.infrastructure.file.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.media.SchemaProperty;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 App - 文件存储")
@Primary
@RestController
@RequestMapping("/infra/file")
@Validated
@Slf4j
public class AppFileController {
    @Resource
    private FileService fileService;

    @PostMapping("/upload")
    @Operation(summary = "上传文件", description = "multipart/form-data 格式，文件数据参数名为“file”，文件名称参数名为“path”")
    @io.swagger.v3.oas.annotations.parameters.RequestBody(content = {@Content(
            mediaType = "multipart/form-data",
            schema = @Schema(type = "object"),
            schemaProperties = {
                    @SchemaProperty(
                            name = "file",
                            schema = @Schema(
                                    type = "string",
                                    format = "binary",
                                    requiredMode = Schema.RequiredMode.REQUIRED
                            )
                    )
            }
    )})
    @PermitAll //上传文件无需登录，如意见反馈场景
    public CommonResult<String> uploadFile(@Valid AppFileUploadReqVO uploadReqVO) throws Exception {
        MultipartFile file = uploadReqVO.getFile();
        if (file == null) {
            return error(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), "未获取到文件数据");
        }
        byte[] contentData = IoUtil.readBytes(file.getInputStream());
        String contentType = file.getContentType();
        String directory = uploadReqVO.getDirectory();
        return success(fileService.createFile(contentData, file.getOriginalFilename(), directory, contentType));
    }

    @GetMapping("/presigned-url")
    @Operation(summary = "获取文件预签名地址", description = "模式二：前端上传文件：用于前端直接上传七牛、阿里云 OSS 等文件存储器")
    @Parameter(name = "name", description = "文件名称", required = true)
    @Parameter(name = "directory", description = "文件目录")
    @PermitAll //上传文件无需登录，如意见反馈场景
    public CommonResult<FilePresignedUrlRespVO> getFilePresignedUrl(@RequestParam(value = "name") String name, @RequestParam(value = "directory", required = false) String directory) {
        return success(fileService.getFilePresignedUrl(name, directory));
    }

    @PostMapping("/create")
    @Operation(summary = "创建文件", description = "模式二：前端上传文件：配合 presigned-url 接口，记录上传了上传的文件")
    @PermitAll //上传文件无需登录，如意见反馈场景
    public CommonResult<Long> createFile(@Valid @RequestBody FileCreateReqVO createReqVO) {
        return success(fileService.createFile(createReqVO));
    }

}
